Skip to content

Enable Cloudflare after-response tasks with gated waitUntil - #512

Open
prk-Jr wants to merge 2 commits into
feature/476-after-response-tasksfrom
feature/478-cloudflare-after-response
Open

prk-Jr wants to merge 2 commits into
feature/476-after-response-tasksfrom
feature/478-cloudflare-after-response

Conversation

@prk-Jr

@prk-Jr prk-Jr commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Enable RequestContext::after_response(budget, task) on Cloudflare Workers so background work starts after terminal response egress without delaying delivery.
  • Retain tasks with gated waitUntil promises, give each task a fresh outbound callback scope, and cancel pending work at min(budget, 30 seconds) after release.
  • Report after-response-tasks as BoundedCooperative for Cloudflare and document Workers lifetime limits, registration closure, and lack of persistence or retries.

Stacked on #509 (feature/476-after-response-tasks, base c3ba7e56), which supplies the portable task API and response-egress foundation. This PR targets that branch; its changes are the Cloudflare implementation of #478.

Changes

Crate / File Change
edgezero-adapter-cloudflare/src/after_response.rs, src/lib.rs Add the request-local release gate and bounded local task scheduler; reject registration after release.
edgezero-adapter-cloudflare/src/request.rs, src/response.rs Install the scheduler in shared dispatch and keep release ownership through native handoff, stream termination, fallback, cancellation, and abandoned dispatch.
edgezero-adapter-cloudflare/src/cli.rs Advertise BoundedCooperative support and update the capability test.
edgezero-adapter-cloudflare/tests/contract.rs and colocated tests Cover service/prebuilt dispatch, streaming release, budgets, stale handles, request isolation, fresh outbound scopes, timeout cancellation, and terminal error paths.
Cloudflare, capability, and handler guides; CHANGELOG.md Describe effective budgets, platform limits, and outbound request construction inside tasks.
scripts/check_outbound_docs_contract.mjs Keep the documented capability matrix aligned with adapter reporting.
Design and implementation documents Record the contract, architecture, verification scope, and implementation plan.

Closes

Closes #478

Test plan

Local checks used Rust 1.98.1 and Node.js 24.20.0. GitHub CI remains pending.

  • cargo test --workspace --all-targets
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo fmt --all -- --check
  • cargo check --workspace --all-targets --features "fastly cloudflare spin"
  • WASM builds: wasm32-wasip1 (Fastly) / wasm32-wasip2 (Spin) / wasm32-unknown-unknown (Cloudflare) — Cloudflare Worker built and Spin target checked; Fastly target build not rerun for this Cloudflare change.
  • examples/app-demo workspace: cd examples/app-demo && cargo test --workspace --all-targets
  • Docs build: cd docs && npm run lint && npm run format && npm run build
  • Manual testing via edgezero serve --adapter axum — runtime verification used Cloudflare below.
  • Other: Cloudflare native crate tests; strict Cloudflare WASM Clippy with cloudflare and cloudflare,test-utils; Cloudflare WASM unit/contract suites and browser ingress suite; outbound documentation contract check.
  • Negative gate control: deliberately bypassing release in an isolated copy makes the streaming contract test fail because task cancellation occurs while the stream is still active (1, expected 0).
  • Public run_app consumer under local Wrangler 4.83/workerd: response-first return about 3 ms versus an inline-delay control about 306 ms; delayed completion, streaming/full post-egress budget, pending-task drop, fresh outbound transport and old-scope revocation, 204 handoff, 500 fallback, and overlapping request gates all passed.

The local workerd run exercises the actual Worker artifact. Hosted Cloudflare deployment was not exercised. Budgets are cooperative, and Workers independently limits the shared waitUntil lifetime; these checks do not establish durable completion. Panic isolation requires an unwinding build; an aborting WASM panic can terminate the invocation.

Checklist

  • Changes follow CLAUDE.md conventions
  • No Tokio deps added to core or adapter crates
  • Route params use {id} syntax (not :id)
  • Types imported from edgezero_core (not http crate)
  • Store wiring goes through KvRegistry / ConfigRegistry / SecretRegistry (not the legacy single-handle setters) — existing store wiring is unchanged
  • New code has tests
  • No secrets or credentials committed

@prk-Jr prk-Jr self-assigned this Nov 18, 2026
@prk-Jr
prk-Jr marked this pull request as ready for review November 18, 2026 09:28
@prk-Jr prk-Jr linked an issue Nov 18, 2026 that may be closed by this pull request
2 tasks
prk-Jr added a commit that referenced this pull request Nov 18, 2026
Installing a rejecting Spin scheduler changes a zero budget from 501 to
500: core now finds a handle and rejects the budget before the
scheduler, as it already does on Axum and Fastly. The guides, changelog
and spec claimed every Spin registration returns 501 and that the zero
budget case was unchanged. Correct them and pin the 500 with a Spin unit
test.

Also note that core pre-dispatch hooks can replace or remove the handle
(removal falls back to core's 501), that PR #512 overlaps on the
CHANGELOG line as well as the capabilities bullet, and point the #509
design at the follow-up.
@aram356
aram356 added this pull request to stack #511 November 19, 2026 05:03
aram356 added a commit that referenced this pull request Nov 19, 2026
#509 (2620377) adds bounded work after the response on Fastly and Axum, drained on Axum SIGTERM (EZI-1, #476). #512 (cdc232c) runs it on Cloudflare through waitUntil (#478), and #515 (ec347a2) reports Spin's capability error (#483). The line now stacks on #517 and each PR merged the one below it, so this merge is clean.
@aram356

aram356 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

For Trusted Server's identity integration chain, this PR's base #509 now stacks on #517. cdc232c8 merges #509's new head 2620377a. The one conflict was adapter-cloudflare/src/request.rs's dispatch_with_handles: this PR's after_response::arm(..) and CloudflareEgressRuntime::with_after_response(..) now sit alongside the base's geo and client-info capture. No behavior or test changes.

Checked locally at cdc232c8: cargo fmt, workspace clippy with all features, both Fastly clippy runs, the wasm feature check, cargo test --workspace --all-targets (3,116 passed), Cloudflare clippy on wasm32-unknown-unknown, and the Cloudflare wasm tests in Node (lib 143, contract 38, including this PR's after-response tests), all passing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

After-response work on Cloudflare (waitUntil)

2 participants